package stringparser;

import java.io.FileWriter;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

public class StringParser {

    private void log(String s) {
        System.out.println("\t" + s);
    }
    private void log1(String s) {
        System.out.println("\t\t" + s);
    }
    private void log2(String s) {
        System.out.println("\t\t\t" + s);
    }

    private void writeFile(RandomAccessFile file, String s) throws Exception {
        file.writeBytes(s);
    }
    private void writeFile(RandomAccessFile file, int i) throws Exception {
        file.writeInt(i);
    }

    private void writeFile(FileWriter file, String s) throws Exception {
        file.write(s);
    }

    private void parseFile(String filename) {
        String[] worksheetsNames = null;
        String[] languagesNames = null;
        List[] stringId = null;
        List[] stringLanguage = null;

        int worksheetsCant = 0;
        int languagesCant = 0;
        int actualWorksheet = 0;

        try {
            RandomAccessFile file = new RandomAccessFile(filename, "r");

            while(file.getFilePointer() < file.length()) {
                String line = file.readLine();
                if(line.indexOf("Worksheet ss") > 0) {
                    worksheetsCant++;
                    languagesCant = 0;
                }
                else if(line.indexOf("::") > 0) {
                    languagesCant++;
                }
            }
            log("worksheetsCant: " + worksheetsCant);
            log("languagesCant: " + languagesCant);
            worksheetsNames = new String[worksheetsCant];
            stringLanguage = new List[languagesCant * worksheetsCant];
            stringId = new List[worksheetsCant];
            languagesNames = new String[languagesCant];

            file.seek(0);
            String line = file.readLine();
            while(actualWorksheet < worksheetsCant) {
                if(line.indexOf("Worksheet ss") > 0) {
                    worksheetsNames[actualWorksheet] = line.substring(line.indexOf("\"")+1, line.lastIndexOf("\""));
                    stringId[actualWorksheet] = new ArrayList();
                    log("parse worksheet " + worksheetsNames[actualWorksheet]);

                    while(line.indexOf("</Table>") < 0) {
                        if(line.indexOf("<Row>") > 0) {
                            line = file.readLine();
                            while(line.indexOf("</Row>") < 0) {
                                if(line.indexOf(":langs:") > 0) {
                                    line = file.readLine();
                                    for(int i=0; i<languagesNames.length; i++) {
                                        languagesNames[i] = line.substring(line.indexOf("::")+2, line.lastIndexOf("::"));
                                        stringLanguage[actualWorksheet * languagesCant + i] = new ArrayList();
                                        log1("parse language " + languagesNames[i]);
                                        line = file.readLine();
                                    }
                                }
                                else if(line.indexOf("<Cell>") >= 0) {
                                    String id = worksheetsNames[actualWorksheet] + "_" + line.substring(line.indexOf("\">")+2, line.lastIndexOf("</D"));
                                    log2(id);
                                    stringId[actualWorksheet].add(id);
                                    for(int i=0; i<languagesNames.length; i++) {
                                        line = file.readLine();
                                        String text = line.substring(line.indexOf("\">")+2, line.lastIndexOf("</D"));
                                        //log2(text);
                                        stringLanguage[actualWorksheet * languagesCant + i].add(text);
                                        //System.out.println("2-" + line);
                                    }
                                }
                                line = file.readLine();
                                //System.out.println("1-" + line);
                            }
                        }
                        line = file.readLine();
                        //System.out.println("0-" + line);
                    }

                    while(line.indexOf("</Worksheet>") < 0) {
                        line = file.readLine();
                    }
                    actualWorksheet++;
                }
                line = file.readLine();
            }
            file.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }

        try {
            FileWriter file = null;
            log("creating Text.java");
            for(int i=0; i<stringId.length; i++) {
                log1("creating interface " + worksheetsNames[i]);
                file = new FileWriter("Text.java", true);
                writeFile(file, "\ninterface " + worksheetsNames[i]);
                writeFile(file, "\n{");
                for(int w=0; w<stringId[i].size(); w++) {
                    writeFile(file, "\n\tpublic static int " + stringId[i].get(w) + " = " + w + ";");
                }
                writeFile(file, "\n}\n\n");
                file.close();
            }
            RandomAccessFile binary_file;
            RandomAccessFile offset_file;
            for(int w=0; w<worksheetsNames.length; w++) {
                for(int l=0; l<languagesNames.length; l++) {
                    log("writing file " + worksheetsNames[w]+"_"+languagesNames[l]+".lang");
                    binary_file = new RandomAccessFile(worksheetsNames[w]+"_"+languagesNames[l]+".lang", "rw");
                    offset_file = new RandomAccessFile(worksheetsNames[w]+"_"+languagesNames[l]+"_offset.lang", "rw");
                    int t;
                    int off = 0;
                    for(t=0; t<stringLanguage[w * languagesCant + l].size(); t++) {
                        writeFile(binary_file, "" + stringLanguage[w * languagesCant + l].get(t) + "");
                        writeFile(offset_file, off);
                        off += (stringLanguage[w * languagesCant + l].get(t)).toString().length();
                    }
                    log1(t + " strings writed");
                    log1(off + " bytes");
                    binary_file.close();
                    offset_file.close();
                }
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String file = "";
        if(args.length == 2 && args[0].equals("-f")) {
            file = args[1];
            System.out.println("parse file " + file);
        }
        else {
            System.out.println("Parameters error.");
            System.out.println("use: java StringParser -f file");
            return;
        }
        new StringParser().parseFile(file);
    }

}
